"""
@name : 链表反转
@author : zhaosiyuan
@projectname: PythonProject
@time: 2022/6/8
"""
# 实现链表的反转
# class Node:
#     def reverseList(self, head):
#         cur, pre = head, None
#         while cur:
#             nextNode = cur.next #暂存下一个节点
#             cur.next = pre # 反转指向，将当前的指向pre
#             pre = cur # 将当前节点存在pre  暂存当前节点
#             cur = nextNode # 指向下一个节点
#         return pre
#         print(list)
#
# list = [1,2,3,4,5]
# list = reversed(list)

# 完成用户注册，登录，购物功能
import json
class User():
    def __init__(self, name, passwd):
        self.name = name
        self.passwd = passwd

    # def __str__(self):
    #     return 'username:{}'

class Function():
    # 注册新用户
    with open('usersInfo', 'a') as f:
        if f.read().strip():
            # 将光标移回最开始
            f.seek(0)
            total_user = json.loads(f.read())
        else:
            total_user = {}

    def register(self):
        flag = True
        while flag:
            username = input('请输入您的用户名：')
            passwd = input('请输入密码：')
            # oneuser = {username: passwd}
            if username == ' ' or passwd == ' ':
                print("用户名或密码不能为空")
                flag = False
            elif username in self.total_user.keys():
                print('用户已存在')
            else:
                self.total_user[username] = {"passwd": passwd}
                # total_user.update(oneuser)
                # total_user = json.dumps(total_user).strip()
                with open('usersInfo', 'a') as f:
                    f.write(json.dumps(self.total_user))
                    print("恭喜您用户注册成功！")
                    flag = False

    # 用户登录
    def login(self):
        flag = True
        user = input('请输入您的用户名：')
        pwd = input('请输入密码：')
        with open('usersInfo', 'r') as f2:
            while flag:
                try:
                    for line in f2:
                        dic = json.loads(line)
                        for keys, values in dic.items():
                            if keys == user and values == pwd:
                                print("登录成功！")
                                flag = False
                                break
                except EOFError:
                    print("登录失败")
                    break

    # 充值
    def top_up(self):
        self.money = int(input("请输入您的充值金额："))

        print(f"您已成功充值{self.money}元，账户余额{self.money}元")
        return self.money

    # 购买
    def buy_goods(self, good, qty):
        if qty > good.store:
            print("库存不足！")
        else:
            pay = qty * good.price
            if self.money >= pay:
                self.money -= pay
                good.store -= qty
                print(f"您已花费{pay}元，{self.name}剩余{self.money}元",
                    f"{good.product_name}剩余库存{good.store}斤")
            else:
                print("余额不足")
    # 查看个人购买信息
    def check(self):
        # self.name = username
        # self.
        pass


# 定义一个商品类
class Goods():
    def __init__(self, classify, product_name, price, store):
        self.classify = classify
        self.product_name = product_name
        self.price = price
        self.store = store
        self.__fool = []

#     存储商品信息
    def supermarket_inventory(self):
        self.__fool.append()
        print(self.__fool)



# 主函数，包含菜单
def main():
    # user = User()
    print("欢迎来到小赵小卖部".center(30, ' '))
    while True:
        print("*"*30 + '\n' + "注册请按1" + '\n' + "登录请按2" + '\n' + '充值请按3' + '\n'
              + '购买请按4' + '\n' + "查看用户信息请按5" + '\n' + '退出请按6' + '\n'
              + '查看商品请按7' + '\n' + "*"*30 + '\n')
        select = input("请输入您的选择：")
        if select == '1':
            opt = Function.register(1)
        elif select == '2':
            opt = Function.login(2)
        elif select == '3':
            opt = Function.top_up(3)
        elif select == '4':
            opt = Function.buy_goods(1,apple, 3)
        elif select == '5':
            pass
        elif select == '6':
            print('欢迎下次光临！')
            break

apple = Goods("fruit", "apple", 7, 30)
orange = Goods("fruit", "orange", 4, 100)
# apple.supermarket_inventory()
# orange.supermarket_inventory()
main()



# user1 = Function()
# # user1.register()
# user1.login()
# n, p = user1.login.user, user1.login.pwd
# user1 = User(n, p)
# user1.top_up(500)
# user1.buy_goods()
# apple = Goods("fruit", "apple", 7, 30)
# orange = Goods("fruit", "orange", 4, 100)
#
# user1.buy_goods(apple, 3)